﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CXMLStudioIDE.WinFormsUI.Docking;
using CXMLStudioIDE.Common.Entities;
using CXMLStudioIDE.DataAccessLayer;

namespace CXMLStudioIDE.Forms
{
    public partial class frmProjectProperties : DockContent
    {
        private Project m_project;
        private int m_selected_project;
        private MainForm m_parent;

        public frmProjectProperties(Form parent,int i_TreeViewID)
        {
            m_parent = (MainForm)parent;
            InitializeComponent();
            m_selected_project = i_TreeViewID;
            m_project = m_parent.m_SolutionManager.m_solution.Projects.GetProjectByTreeviewID(m_selected_project);
            txtProjectName.Text = m_project.Name;
            CXMLStudioIDE.DataAccessLayer.CXMLStudioDAL dal = new CXMLStudioDAL();
            dsContext = dal.GetContext();
            cmbContext.DisplayMember = dsContext.Tables[0].Columns[1].ColumnName.ToString();
            cmbContext.ValueMember = dsContext.Tables[0].Columns[0].ColumnName.ToString();
            cmbContext.DataSource = dsContext.Tables[0];
            for (int i = 0; i < cmbContext.Items.Count; i++)
            {
                if ((cmbContext.Items[i] as DataRowView).Row[1].ToString().Equals(m_project.Context.Name))
                {
                    cmbContext.SelectedIndex = i;
                }
            }
            dsScope = dal.GetScope();
            cmbScope.DisplayMember = dsScope.Tables[0].Columns[1].ColumnName.ToString();
            cmbScope.ValueMember = dsScope.Tables[0].Columns[0].ColumnName.ToString();
            cmbScope.DataSource = dsScope.Tables[0];
            for (int i = 0; i < cmbScope.Items.Count; i++)
            {
                if ((cmbScope.Items[i] as DataRowView).Row[1].ToString().Equals(m_project.Scope.Name))
                {
                    cmbScope.SelectedIndex = i;
                }
            }
        }
        public void SaveChanges()
        {
            if (!((DataRowView)cmbContext.Items[cmbContext.SelectedIndex]).Row[1].ToString().Equals(m_project.Context.Name))
            {
                MessageBox.Show("Context Changed!!");
            }
            if (!((DataRowView)cmbScope.Items[cmbScope.SelectedIndex]).Row[1].ToString().Equals(m_project.Scope.Name))
            {
                MessageBox.Show("Scope Changed!!");
            }
        }

        private void frmProjectProperties_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult result = MessageBox.Show(this,"Save Project Properties?", "CXMLStudioIDE - Confirm save Project Properties", MessageBoxButtons.YesNoCancel,
                MessageBoxIcon.Question);
            if (result == DialogResult.Yes || result == DialogResult.No)
            {
                if (result == DialogResult.Yes)
                {
                    //TODO: Call save solution here
                }
                this.Dispose();
            }
        }
    }
}
